package com.zhongkai.app.service.impl;

import com.zhongkai.app.service.IBaseDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Repository("baseDao")  //此注解不可少
public class IBaseDaoImpl<T> implements IBaseDao<T> {

    /*    @Resource//根据类型注入spring工厂中的会话工厂对象sessionFactory
        public void setMySessionFactory(SessionFactory sessionFactory){
            super.setSessionFactory(sessionFactory);
        }*/


    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;

    }

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    @Override
    public Serializable save(T o) {
        return this.getCurrentSession().save(o);
    }

    @Override
    public T get(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        //       q.setCacheable(true); //开启缓存
        List<T> l = q.list();
        if (l != null && l.size() > 0) {
            return l.get(0);
        }
        return null;
    }


    @Override
    public T get(String hql, Map<String, Object> params) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        List<T> l = q.list();
        if (l != null && l.size() > 0) {
            return l.get(0);
        }
        return null;
    }

    @Override
    public void delete(T o) {
        this.getCurrentSession().delete(o);
    }

    @Override
    public void update(T o) {
        this.getCurrentSession().update(o);
    }

    @Override
    public void saveOrUpdate(T o) {
        this.getCurrentSession().saveOrUpdate(o);
    }

    @Override
    public void update1(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        q.executeUpdate();

        return;
        //System.out.println(i);
    }

    @Override
    public List<T> find(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        // q.setCacheable(true); //开启缓存
        // q.iterate();
        return q.list();
    }

    @Override
    public List<T> findThree(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        q.setMaxResults(3);
        q.setFirstResult(0);
        return q.list();
    }

    @Override
    public List<T> find(String hql, Map<String, Object> params) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }

        return q.list();
    }

    @Override
    public List<T> findSQL(String hql, Map<String, Object> params) {
        Query q = this.getCurrentSession().createSQLQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        return q.list();
    }


    @Override
    public List<T> find(String hql, Map<String, Object> params, int page, int rows) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
    }

    @Override
    public List<T> find(String hql, int page, int rows) {
        Query q = this.getCurrentSession().createQuery(hql);
        return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
    }

    @Override
    public T count(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        return (T) q.uniqueResult();
    }

    @Override
    public T count(String hql, Map<String, Object> params) {
        Query q = this.getCurrentSession().createQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                q.setParameter(key, params.get(key));
            }
        }
        return (T) q.uniqueResult();
    }

    @Override
    public List<T> findTen(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        q.setMaxResults(3);
        q.setFirstResult(0);
        return q.list();
    }

    @Override
    public T findOne(String hql) {
        Query q = this.getCurrentSession().createQuery(hql);
        q.setFirstResult(0);
        q.setMaxResults(1);
        List<T> l = q.list();

        if (l != null && l.size() > 0) {
            return l.get(0);
        }
        return null;
    }


    @Override
    public List<T> findSql(String sql) {
        Query q = this.getCurrentSession().createSQLQuery(sql);
        return q.list();
    }

    @Override
    public boolean updateSql(String sql) {
        Query q = this.getCurrentSession().createSQLQuery(sql);
        System.out.println(q);
        return true;

    }

    @Override
    public List<T> findThe(String hql) {
        Query q = this.getCurrentSession().createQuery(hql).setMaxResults(3);
        return q.list();
    }


}
